// 图表联动管理模块
const ChartSync = {
  // 存储所有图表实例
  chartInstances: [],
  
  // 注册图表实例
  registerChart: function(chartInstance) {
    if (chartInstance && this.chartInstances.indexOf(chartInstance) === -1) {
      this.chartInstances.push(chartInstance);
    }
  },

  // 移除图表实例
  unregisterChart: function(chartInstance) {
    const index = this.chartInstances.indexOf(chartInstance);
    if (index !== -1) {
      this.chartInstances.splice(index, 1);
    }
  },

  // 连接所有图表实现联动
  connectCharts: function() {
    if (this.chartInstances.length > 0) {
      // 使用ECharts的connect方法连接所有图表
      echarts.connect(this.chartInstances);
      console.log(`已连接 ${this.chartInstances.length} 个图表实现联动`);
    }
  },

  // 断开图表连接
  disconnectCharts: function() {
    if (this.chartInstances.length > 0) {
      echarts.disconnect(this.chartInstances);
      console.log('已断开图表联动');
    }
  },

  // 重新调整所有图表大小
  resizeAllCharts: function() {
    this.chartInstances.forEach(chart => {
      if (chart && typeof chart.resize === 'function') {
        chart.resize();
      }
    });
  },

  // 清空所有图表
  clearAllCharts: function() {
    this.chartInstances.forEach(chart => {
      if (chart && typeof chart.clear === 'function') {
        chart.clear();
      }
    });
  },

  // 销毁所有图表
  disposeAllCharts: function() {
    this.chartInstances.forEach(chart => {
      if (chart && typeof chart.dispose === 'function') {
        chart.dispose();
      }
    });
    this.chartInstances = [];
  },

  // 获取图表数量
  getChartCount: function() {
    return this.chartInstances.length;
  },

  // 检查图表是否已注册
  isChartRegistered: function(chartInstance) {
    return this.chartInstances.indexOf(chartInstance) !== -1;
  }
};

// 工具函数
const ChartUtils = {
  // 节流函数
  throttle: function(func, limit) {
    let inThrottle;
    return function() {
      const args = arguments;
      const context = this;
      if (!inThrottle) {
        func.apply(context, args);
        inThrottle = true;
        setTimeout(() => inThrottle = false, limit);
      }
    };
  },

  // 防抖函数
  debounce: function(func, wait) {
    let timeout;
    return function executedFunction(...args) {
      const later = () => {
        clearTimeout(timeout);
        func(...args);
      };
      clearTimeout(timeout);
      timeout = setTimeout(later, wait);
    };
  },

  // 格式化日期
  formatDate: function(dateStr) {
    if (!dateStr) return '';
    return dateStr.replace(/\//g, '-');
  },

  // 降采样数据
  downsampleData: function(data, targetPoints) {
    if (!data || data.length <= targetPoints) return data;
    
    const step = Math.ceil(data.length / targetPoints);
    const result = [];
    
    for (let i = 0; i < data.length; i += step) {
      result.push(data[i]);
    }
    
    return result;
  },

  // 限制数据点数量
  limitDataPoints: function(data, maxPoints) {
    if (!data || data.length <= maxPoints) return data;
    
    // 使用均匀采样
    const step = Math.floor(data.length / maxPoints);
    const result = [];
    
    for (let i = 0; i < data.length; i += step) {
      result.push(data[i]);
      if (result.length >= maxPoints) break;
    }
    
    // 确保包含最后一个点
    if (result[result.length - 1] !== data[data.length - 1]) {
      result[result.length - 1] = data[data.length - 1];
    }
    
    return result;
  }
};

// 导出到全局
window.ChartSync = ChartSync;window.ChartUtils = ChartUtils; 
